package com.shop.user.service.user.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.modular.constant.BaseCodeDesc;
import com.modular.exception.CodeDesc;
import com.modular.exception.ModularException;
import com.modular.result.CodeDescUtils;
import com.modular.result.PageResult;
import com.shop.user.constants.Constants;
import com.shop.user.dao.SysUserMapper;
import com.shop.user.model.SysUser;
import com.shop.user.service.user.IUserService;
import com.shop.user.utils.PageUtils;
import com.shop.user.vo.user.DataSttsReqVo;
import com.shop.user.vo.user.UserPageReq;
import com.shop.user.vo.user.UserPageResVo;
import com.shop.user.vo.user.UserReqVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    @Qualifier("modularSnowflake")
    private Snowflake snowflake;
//    @Autowired
    private SysUserMapper sysUserMapper;
    @Override
    public void saveOrUpdate(UserReqVo reqVo) {
        SysUser dbUser = null;
        if(StrUtil.isNotBlank(reqVo.getIidd())){
            dbUser = sysUserMapper.selectById(reqVo.getIidd());
        }else{
            LambdaQueryWrapper<SysUser> query = new LambdaQueryWrapper<>();
            query.eq(SysUser::getUserAccount,reqVo.getUserAccount());
            dbUser = sysUserMapper.selectOne(query);
        }
        SysUser modify = new SysUser();
        BeanUtils.copyProperties(reqVo,modify);
        modify.setId(dbUser.getId());
        modify.setCreateDate(DateUtil.date());
        if(ObjectUtil.isNull(dbUser)){
            modify.setDataStts(Constants.DataStts.ENABLED);
            modify.setId(snowflake.nextIdStr());
            modify.setCreateDate(modify.getModifyDate());
            sysUserMapper.insert(modify);
        }else{
            sysUserMapper.updateById(modify);
        }
    }

    /**
     * 修改状态
     * @param reqVo
     */
    @Override
    public void modifyStts(DataSttsReqVo reqVo){
        SysUser dbUser = sysUserMapper.selectById(reqVo.getIidd());
        if(ObjectUtil.isNull(dbUser)){
            throw new ModularException(BaseCodeDesc.DEFATUL_MSG,"用户错误");
        }
        SysUser modify = new SysUser();
        modify.setId(dbUser.getId());
        modify.setDataStts(reqVo.getDataStts());
        modify.setModifyDate(DateUtil.date());
        sysUserMapper.updateById(modify);
    }
    /**
     * 分页查询数据
     * @param reqVo
     * @return
     */
    @Override
    public PageResult<UserPageResVo> findPage(UserPageReq reqVo)
    {
        IPage<?> pageReq = Page.of(reqVo.getPageNum(), reqVo.getPageSize());
        Page<UserPageResVo> page = sysUserMapper.findPage(pageReq,reqVo);
        return PageUtils.cover(page);
    }
}
